<template>
  <div>
    <!-- 搜索条件 -->
    <div id="foldDiv">
      <el-form style="margin-bottom:9px" :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
        <span class="el-form-item__label">类型</span>
        <el-select v-model="queryParams.type" style="width:90px;margin-right: 10px" size="mini" @change="getList" clearable>
          <el-option v-for="dict in dict.type.stock_up_statistics_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
        <el-button type="danger" icon="el-icon-s-data" size="mini" @click="handleGenerateKLine">EchartsK</el-button>
        <el-button type="danger" icon="el-icon-s-data" size="mini" @click="handleGenerateMyKLine">MyK</el-button>
        <el-button size="mini" type="primary" @click="handleAverageLineType(1)">均线向上</el-button>
        <el-button size="mini" type="info" @click="handleAverageLineType(2)">MA5>10</el-button>
        <el-button size="mini" type="warning" @click="handleAverageLineType(3)">MA5>20</el-button>
        <el-button size="mini" type="danger" @click="handleAverageLineType(4)">MA5>30</el-button>
        <el-button size="mini" type="success" @click="handleAverageLineType(5)">MA5>60</el-button>
        <el-button size="mini" type="primary" @click="handleAverageLineType(6)">MA5>125</el-button>
        <el-button size="mini" style="color: #FFFFFF;background-color: #d521d0" @click="handleAverageLineType(7)">MA5>250</el-button>
        <el-button icon="el-icon-back" size="mini" @click="back"></el-button>&nbsp;
      </el-form>
    </div>

    <!--K线图-->
    <div v-if="isShowKLine">
      <template v-for="(item, index) in stockCodeKLine">
        <el-card shadow="always" style="margin-top:20px">
          <div :id="'d'+item.split('_')[0]" style="width: 100%;height: 600px"></div>
        </el-card>
      </template>
    </div>

    <!--我的K线图-->
    <div v-if="isShowMyKLine">
      <template v-for="(item) in stockCodeKLine">
        <el-card style="margin-top:20px">
          <div :id="'d'+item.split('_')[0]" style="width: 100%;height: 600px"></div>
        </el-card>
      </template>
    </div>

    <!-- 表格列表 -->
    <pl-table id="tableDiv" v-loading="loading" @sort-change="handleSortChange"
              ref="plTable"
              :height="tableHeight"
              :data="stockUpStatisticsList"
              selectTrClass="selectTr"
              header-drag-style
              :dataChangesScrollTop="false"
              @selection-change="handleSelectionChange"
              fixedColumnsRoll
              inverse-current-row
              bigDataCheckbox
              use-virtual
    >
      <pl-table-column type="selection" width="60"/>
      <pl-table-column label="股票" width="70" prop="stockCode"/>
      <pl-table-column label="股票名称" width="80" prop="stockName"/>
      <pl-table-column label="统计" width="50" prop="statisticsDays"/>
      <pl-table-column label="倍量" width="100" sortable="custom" prop="volumeTimes"/>
      <pl-table-column label="类型" width="75" sortable="custom" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.stock_up_statistics_type" :value="scope.row.type"/>
        </template>
      </pl-table-column>
      <pl-table-column label="上涨" width="75" sortable="custom" prop="riseDays"/>
      <pl-table-column label="涨停" width="75" sortable="custom" prop="riseStopDays"/>
      <pl-table-column label="连涨" width="75" sortable="custom" prop="continueRiseDays"/>
      <pl-table-column label="幅度" width="75" sortable="custom" prop="continueRiseRate"/>
      <pl-table-column label="连续涨停" width="100" sortable="custom" prop="continueRiseStopDays"/>
      <pl-table-column label="下跌" width="75" sortable="custom" prop="fallDays"/>
      <pl-table-column label="跌停" width="75" sortable="custom" prop="fallStopDays"/>
      <pl-table-column label="连续下跌" width="100" sortable="custom" prop="continueFallDays"/>
      <pl-table-column label="幅度" width="75" sortable="custom" prop="continueFallRate"/>
      <pl-table-column label="连续跌停" width="100" sortable="custom" prop="continueFallStopDays"/>
      <pl-table-column label="K线名称" prop="kLineName"/>
    </pl-table>

    <!-- 分页-->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

  </div>
</template>

<script>
import {listStockUpStatistics, averageLineType} from '@/api/eye/stockUpStatistics'
import {renderKLine} from '@/api/eye/kline'
import {getHistoryPriceByStockCode} from '@/api/eye/stockPrice'
import {initDragonKLine} from "@/api/eye/myKLine";
import {PlTable, PlTableColumn} from 'pl-table';
import 'pl-table/themes/index.css'
import 'pl-table/themes/plTableStyle.css'

export default {
  name: 'StatisticsA',
  dicts: ['stock_up_statistics_type'],
  components: {
    PlTable,
    PlTableColumn
  },
  data() {
    return {
      tableHeight: 200,
      // K线图是否显示
      isShowKLine: false,
      // 展示我的K线
      isShowMyKLine: false,
      // 遮罩层
      loading: true,
      // 选中数组
      selectedValues: [],
      stockCodeKLine: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股票指标统计信息表格数据
      stockUpStatisticsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        stockCode: null,
        stockName: null,
        type: null,
        statisticsDays: null,
        riseDays: null,
        fallDays: null,
        continueRiseDays: null,
        continueRiseRate: null,
        continueFallDays: null,
        continueFallRate: null,
        riseStopDays: null,
        fallStopDays: null,
        continueRiseStopDays: null,
        continueFallStopDays: null,
        kLineName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getTableMaxHeight()
    let _this = this
    window.onresize = function () {
      console.log('onresize 统计')
      //页面改变重新自适应高度
      _this.getTableMaxHeight()
    }
  },
  methods: {
    //获取容器当前高度，重设表格的最大高度
    getTableMaxHeight() {
      this.$nextTick(function () {
        this.tableHeight = window.innerHeight - 270
        console.log('统计 重置高度:' + this.tableHeight)
      })
    },
    /** 查询股票指标统计信息列表 */
    getList() {
      this.loading = true
      this.queryParams.pageSize = 50
      listStockUpStatistics(this.queryParams).then(response => {
        this.stockUpStatisticsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 排序
    handleSortChange(column) {
      // 排序字段
      let sortColumn = column.prop
      // 升序还是降序
      let sortOrder = column.order
      this.queryParams.orderByColumn = sortColumn
      if (sortOrder === 'ascending') {
        console.log('升序')
        this.queryParams.isAsc = 'asc'
      } else {
        console.log('降序')
        this.queryParams.isAsc = 'desc'
      }
      this.getList()
    },
    handleAverageLineType(n) {
      console.log(n)
      this.loading = true
      this.queryParams.pageSize = 5000
      this.queryParams.averageLineType = n
      averageLineType(this.queryParams).then(response => {
        this.stockUpStatisticsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 返回 */
    back() {
      this.isShowKLine = false
      this.isShowMyKLine = false
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedValues = selection.map(item => item.stockCode + '_' + item.stockName)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 生成K线图 */
    handleGenerateKLine() {
      this.stockCodeKLine = this.selectedValues
      this.isShowKLine = true
      this.selectedValues.forEach(item => {
        let code = item.split('_')[0]
        getHistoryPriceByStockCode(code).then(response => {
          renderKLine('d' + code, item, response.data, 1)
        })
      })
    },
    /** 生成我的K线图 */
    handleGenerateMyKLine() {
      this.stockCodeKLine = this.selectedValues
      this.isShowMyKLine = true
      this.stockCodeKLine.forEach(item => {
        let code = item.split('_')[0]
        getHistoryPriceByStockCode(code).then(response => {
          initDragonKLine('d' + code, item, response.data.date, response.data.price)
        })
      })
    }
  }
}
</script>
